package com.ymt.bpm.service.mobile;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ymt.bpm.dao.MDao;
import com.ymt.bpm.dao.Mapper;
import com.ymt.bpm.model.PUser;
import com.ymt.bpm.util.Const;
import com.ymt.bpm.util.DhxUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MOrgService {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MDao dao;

    public JSONObject queryUser(String tenantId,
                                String lang,
                                String order,
                                String sort,
                                int offset,
                                int limit,
                                int timezoneOffset,
                                String search) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        if (tenantId!=null && StringUtils.isNumeric(tenantId)) {
            paramMap.put(Const.TENANT_ID, tenantId);
        }
        paramMap.put("order", order);
        paramMap.put("sort", sort);
        paramMap.put("search", search);

        PageHelper.offsetPage(offset, limit);
        List<PUser> users = dao.selectList(Mapper.PUserMapper, "mQueryUser", paramMap);
        PageInfo pageInfo = new PageInfo(users);

        JSONObject rtn = new JSONObject();
        JSONArray rows = DhxUtil.toJsonWithDate(users, lang, timezoneOffset);
        rtn.put(DhxUtil.ROWS, rows);
        rtn.put(DhxUtil.TOTAL_COUNT, pageInfo.getTotal());
        return rtn;
    }

}
